﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures
{
    public class MeasuresGroupItemDetailValueManager : BusinessLogic
    {
        public MEASURE_DETAIL_VALUE Load(decimal VISIT_ID, decimal MEASUER_DETAIL_ID, string DOMAINID)
        {
            return (MEASURE_DETAIL_VALUE)ObjectRow.Load(typeof(MEASURE_DETAIL_VALUE)
                , string.Format("VISIT_ID='{0}' and MEASUER_DETAIL_ID='{1}' and VISIT_ID='{2}'"
                , DOMAINID, MEASUER_DETAIL_ID, VISIT_ID));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Begin">拜访开始时间</param>
        /// <param name="End">拜访结束时间</param>
        /// <param name="CustomerID">客户</param>
        /// <param name="MBUserID">销售代表</param>
        /// <param name="domainid">domainid</param>
        /// <returns></returns>
        public static DataTable GetMeasuresTransactionInfo(string where)
        {
           String   sql = @"SELECT 
P.NAME AS PERSON_NAME,V.STOP_TIME,MI.ID AS MEASURE_ID,MI.NAME AS MEASURE_NAME,
MD.NAME DETAILS,CASE MDV.INPUTTYPE WHEN 6 THEN ''
ELSE MDV.TEXTVALUE END AS VALUE,MDV.PHOTOID, MDV.INPUTTYPE,MD.ISPHOTO
FROM MEASURE_DETAIL_VALUE MDV LEFT JOIN VISITS V ON MDV.VISIT_ID = V.ID 
INNER JOIN PERSONS AS P ON P.ID=V.PERSON_ID 
INNER JOIN MEASURE_ITEM_DETAIL MID ON MDV.MEASUER_DETAIL_ID = MID.MEASUER_DETAIL_ID 
INNER JOIN MEASURE_ITEMS MI ON MI.ID = MID.MEASUER_ITEMS_ID AND MI.ACTIVE = 1
INNER JOIN MEASURE_GROUP MG ON MI.MEASUER_GROUP_ID = MG.ID AND MG.ACTIVE = 1 
INNER JOIN MEASURE_DETAIL MD ON MD.ID = MDV.MEASUER_DETAIL_ID  
WHERE 1=1 " + where;

           sql += " ORDER BY MEASURE_NAME,MID.SEQUENCE";

            return SqlData.OpenSql(sql).Tables[0];
        }

        public static DataTable GetPromotionPersonReport(string mintime, string maxtime, int domainid, string code,string userids)
        {
            DataTable dt = SqlData.OpenSP("SP_PROMOTION_PERSON_REPORT_KONG", mintime, maxtime, domainid, code, userids).Tables[0];
            return dt;
        }
        public static DataTable GetPromotionPersonReport(string mintime, string maxtime, int domainid, string code)
        {
            DataTable dt = SqlData.OpenSP("SP_PROMOTION_PERSON_REPORT", mintime, maxtime, domainid, code).Tables[0];
            return dt;
        }
        public static DataTable GetKPIinfo(string visitid, string measurename)
        {
            DataTable dt=null;
            DataSet ds = SqlData.OpenSP("sp_getKPIinfo", visitid, measurename);
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];
            return dt;
        }

        public static DataTable GetCPRinfo(string visitID, string domainID, string contender)
        {
            DataTable dt = null;
            DataSet ds = SqlData.OpenSP("sp_exportVisitInfo", visitID, domainID, "1", contender);
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];
            return dt;
        }
    }
}
